package DTO;

import java.util.ArrayList;
import java.util.List;

import model.impl.Passenger;
import model.impl.Schedule;
import model.impl.Station;
import model.impl.Ticket;
import model.impl.Train;
import DAO.StationDAO;
import DAO.TrainDAO;
import DAO.impl.StationDAOImpl;
import DAO.impl.TrainDAOImpl;

/**
 * 
 * Class that contains method to create new DTO from entities
 * objects and vice versa - create new entities object from DTO's. 
 * 
 */
public class DTOManager {
	
	/**
	 * Creates a new PassengerDTO from Passenger object
	 * 
	 * @param passenger - a passenger to be converted to DTO
	 * @return PassengerDTO object
	 */
	public  PassengerDTO createPassengerDTO (Passenger passenger) {
		PassengerDTO passengerDTO = new PassengerDTO();
		passengerDTO.setPassengerDateOfBirth(passenger.getPassengerDateOfBirth());
		passengerDTO.setPassengerID(passenger.getPassengerID());
		passengerDTO.setPassengerName(passenger.getPassengerName());
		passengerDTO.setPassengerSurname(passenger.getPassengerSurname());
		if (passenger.getLogin() != null) {
			passengerDTO.setLogin(passenger.getLogin());
		}
		if (passenger.getPassword() != null) {
			passengerDTO.setPassword(passenger.getPassword());
		}
		List<TicketDTO> ticketsList = new ArrayList<TicketDTO>();
		List<Ticket> tickets = passenger.getTickets();
		TicketDTO ticket = null;
		ScheduleDTO scheduleDTO = null;
		if (tickets != null) {
			for(int i = 0; i < tickets.size(); i++) {
				ticket = new TicketDTO();
				scheduleDTO = new ScheduleDTO();
				ticket.setPrice(passenger.getTickets().get(i).getPrice());
				ticket.setTicketID(tickets.get(i).getTicketID());
				scheduleDTO = this.createScheduleDTO(tickets.get(i)
						.getSchedule());
				ticket.setSchedule(scheduleDTO);
				ticketsList.add(ticket);
			}
			passengerDTO.setTickets(ticketsList); 
		}
		return passengerDTO;
	}
	
	
	/**
	 * Creates a new Passenger object from PassengerDTO 
	 * 
	 * @param passengerDTO - a passengerDTO to be converted to Passenger
	 * @return Passenger object
	 */
	public  Passenger createNewPassengerFromDTO (PassengerDTO passengerDTO) {
		Passenger newPassenger = new Passenger();
		newPassenger.setPassengerDateOfBirth(passengerDTO
				.getPassengerDateOfBirth());
		newPassenger.setPassengerID(passengerDTO.getPassengerID());
		newPassenger.setPassengerName(passengerDTO.getPassengerName());
		newPassenger.setPassengerSurname(passengerDTO.getPassengerSurname());
		newPassenger.setLogin(passengerDTO.getLogin());
		newPassenger.setPassword(passengerDTO.getPassword());
		return newPassenger;
	}
	
	/**
	 * Creates a new TicketDTO from Ticket object
	 * 
	 * @param ticket  - a Ticket to be converted to TicketDTO
	 * @return TicketDTO object
	 */
	public  TicketDTO createTicketDTO (Ticket ticket) {
		TicketDTO ticketDTO = new TicketDTO();
		ticketDTO.setPrice(ticket.getPrice());
		ticketDTO.setTicketID(ticket.getTicketID());
		ticketDTO.setSchedule(this.createScheduleDTO(ticket.getSchedule()));
		ticketDTO.setPassenger(this.createPassengerDTO(ticket.getPassenger()));
		return ticketDTO;
	}
	
	/**
	 * Creates a new TrainDTO from Train object
	 * 
	 * @param train  - a Train to be converted to TrainDTO
	 * @return TrainDTO object
	 */
	public  TrainDTO createTrainDTO (Train train) {
		TrainDTO trainDTO = new TrainDTO();
		trainDTO.setPlaces(train.getPlaces());
		trainDTO.setTrainNumber(train.getTrainNumber());
		return trainDTO;
	}
	
	/**
	 * Creates a new StationDTO from Station object
	 * 
	 * @param station  - a Station to be converted to StationDTO
	 * @return StationDTO object
	 */
	public  StationDTO createStationDTO (Station station) {
		StationDTO stationDTO = new StationDTO();
		stationDTO.setStationName(station.getStationName());
		stationDTO.setStationID(station.getStationID());
		return stationDTO;
	}
	
	/**
	 * Creates a new Station object from StationDTO 
	 * 
	 * @param stationDTO - a StationDTO to be converted to Station
	 * @return Station object
	 */
	public  Station createNewStationFromDTO (StationDTO stationDTO) {
		Station station = new Station();
		station.setStationName(stationDTO.getStationName());
		return station;
	}
	
	/**
	 * Creates a new Train object from TrainDTO 
	 * 
	 * @param trainDTO - a TrainDTO to be converted to Train
	 * @return Train object
	 */
	public  Train createNewTrainFromDTO (TrainDTO trainDTO) {
		Train train = new Train();
		train.setPlaces(trainDTO.getPlaces());
		train.setTrainNumber(trainDTO.getTrainNumber());
		return train;
	}

	/**
	 * Creates a new Schedule object from ScheduleDTO 
	 * 
	 * @param scheduleDTO - a ScheduleDTO to be converted to Schedule object
	 * @return Schedule object
	 */
	public  Schedule createNewScheduleFromDTO(ScheduleDTO scheduleDTO) {
		StationDAO stationDAO = new StationDAOImpl();
		TrainDAO trainDAO = new TrainDAOImpl();
		Schedule schedule = new Schedule();
		schedule.setTrain(trainDAO.findTrainByTrainNumber(scheduleDTO.getTrainNumber()));
		schedule.setStation1(stationDAO.findStationByName(scheduleDTO.getStation1()));
		schedule.setStation2(stationDAO.findStationByName(scheduleDTO.getStation2()));
		schedule.setDateOfArrival(scheduleDTO.getDateOfArrival());
		schedule.setDateOfDeparture(scheduleDTO.getDateOfDeparture());
		return schedule;
	}

	/**
	 * Creates a new ScheduleDTO from Schedule object
	 * 
	 * @param schedule  - a Schedule object to be converted to ScheduleDTO
	 * @return ScheduleDTO
	 */
	public  ScheduleDTO createScheduleDTO(Schedule schedule) {
		ScheduleDTO scheduleDTO = new ScheduleDTO();
		scheduleDTO.setDateOfArrival(schedule.getDateOfArrival());
		scheduleDTO.setDateOfDeparture(schedule.getDateOfDeparture());
		scheduleDTO.setStation1(schedule.getStation1().getStationName());
		scheduleDTO.setStation2(schedule.getStation2().getStationName());
		scheduleDTO.setTrainNumber(schedule.getTrain().getTrainNumber());
		return scheduleDTO;
	}
}
